package com.pickgliss.ui.controls
{
	import com.pickgliss.geom.InnerRectangle;
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.utils.ClassUtils;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	public class ProgressBar extends Component
	{
		public static const P_troughStyle:String = "troughStyle";
		public static const P_barStyle:String = "barStyle";
		public static const P_maskStyle:String = "maskStyle";
		public static const P_textStyle:String = "textStyle";
		public static const P_rotationStyle:String = "textStyle";
		public static const P_textInnerRect:String = "textInnerRect";
		
		/**
		 *左到右填充 
		 */		
		public static const FILL_L2R:int = 0;
		/**
		 *右到左填充 
		 */		
		public static const FILL_R2L:int = 1;
		
		/**
		 *左到右挖空
		 */		
		public static const DIG_L2R:int = 2;
		/**
		 *右到左挖空 
		 */		
		public static const DIG_R2L:int = 3;
		
		
		public function ProgressBar()
		{
			super();
		}
		
		private var _trough:DisplayObject;
		private var _troughStyle:String;
		
		private var _bar:DisplayObject;
		private var _barStyle:String;
		
		private var _mask:DisplayObject;
		private var _maskStyle:String;
		
		private var _text:DisplayObject;
		private var _textStyle:String;
		
		private var _progress:Number;
		private var _vertical:Boolean;
		private var _fillType:int;
		private var _shape:Shape;
		
		private var _textInnerRect:InnerRectangle;
		private var _textInnerRectString:String;
		
		override protected function init():void{
			_shape = new Shape();
			_progress = 0;
		}
		
		override protected function addChildren():void{
			super.addChildren();
			if(_trough)addChild(_trough);
			if(_bar)addChild(_bar);
			if(_mask)_bar.mask = _mask;
			if(_text)addChild(_text);
		}
		
		/**
		 * 
		 * @param value 背景外框的矩形
		 * 详细请看 OuterRectangle
		 * 
		 */		
		public function set textInnerRectString(value:String):void
		{
			if(_textInnerRectString == value)return ;
			_textInnerRectString = value;
			_textInnerRect = ClassUtils.CreatInstance(ClassUtils.INNERRECTANGLE,ComponentFactory.parasArgs(_textInnerRectString));
			onPropertiesChanged(P_textInnerRect);
		}
		
		public function set vertical(value:Boolean):void{
			_vertical = value;
			var tempX:Number;
			if(_vertical){
				if(_trough){
					_trough.rotation = -90;
					tempX = _trough.x;
					_trough.x = _trough.y;
					_trough.y = -tempX;
				}
				if(_bar){
					_bar.rotation = -90;
					tempX = _bar.x;
					_bar.x = _bar.y;
					_bar.y = -tempX;
				}
//				if(_text){
//					_text.x = _bar.x;
//					_text.y = _bar.y - (_bar.width + _text.height)/2;
//					_text.width = _bar.height;
//				}
			}else {
//				if(_text){
//					_text.x = _bar.x;
//					if(_bar.height>_text.height){
//						_text.y = _bar.y + (_bar.height - _text.height)/2;
//					} else {
//						_text.y = _bar.y;
//					}
//					_text.width = _bar.width;
//				}
			}
		}
		
		public function get progress():Number{
			return _progress;
		}
		
		public function set progress(value:Number):void{
			if(_progress==value)return;
			if(value>1)value=1;
			_progress = value;
			updateView();
		}
		
		private function updateView():void{
			var tempData:BitmapData;
			_shape.graphics.clear();
			if(_mask){
				tempData = new BitmapData(_mask.width,_mask.height);
				tempData.draw(_mask);
				_shape.graphics.beginBitmapFill(tempData);
			}else {
				_shape.graphics.beginFill(0xffffff,0);
			}
			
			switch(_fillType){
				case FILL_L2R:
					if(!_vertical){
						_shape.graphics.drawRect(x + _bar.x, y + _bar.y, _bar.width * _progress,_bar.height);
					} else {
						_shape.graphics.drawRect(x + _bar.x ,y + _bar.y, _bar.height, - _bar.width * _progress);
					}
					if(_text is TextField)TextField(_text).text = String(Math.round(_progress*100));
					break;
				case FILL_R2L:
					if(!_vertical){
						_shape.graphics.drawRect(x + _bar.x + _bar.width, y + _bar.y, -_bar.width * _progress, _bar.height);
					} else {
						_shape.graphics.drawRect( x + _bar.x, y + _bar.y - _bar.width, _bar.height, _bar.width * _progress);
					}
					if(_text is TextField)TextField(_text).text = String(Math.round(_progress*100));
					break;
				case DIG_L2R:
					if(!_vertical){
						_shape.graphics.drawRect(x + _bar.x + _bar.width,  y + _bar.y, -_bar.width * ( 1 - _progress), _bar.height);
					} else {
						_shape.graphics.drawRect( x + _bar.x, y + _bar.y - _bar.width, _bar.height, _bar.width * (1 - _progress));
					}
					if(_text is TextField)TextField(_text).text = String(Math.round((1-_progress)*100));
					break;
				case DIG_R2L:
					if(!_vertical){
						_shape.graphics.drawRect(x + _bar.x, y + _bar.y, _bar.width * ( 1 - _progress), _bar.height);
					} else {
						_shape.graphics.drawRect(x + _bar.x ,y + _bar.y, _bar.height, -_bar.width * ( 1 - _progress));
					}
					if(_text is TextField)TextField(_text).text = String(Math.round((1-_progress)*100));
					break;
			}
			_shape.graphics.endFill();
			_bar.mask = _shape;
		}
		
		public function set fillType(value:int):void{
			if(_fillType==value)return;
			_fillType = value;
		}
		
		/**
		 *
		 * 槽样式..别想歪了..不是艹  - -!
		 *
		 */
		public function get troughStyle():String
		{
			return _troughStyle;
		}
		
		public function set troughStyle(stylename:String):void
		{
			if (stylename == _troughStyle)
				return;
			_troughStyle = stylename;
			trough = ComponentFactory.Instance.creat(_troughStyle)
			onPropertiesChanged(P_troughStyle);
		}
		
		/**
		 *
		 * @param display 槽
		 *
		 */
		public function set trough(display:DisplayObject):void
		{
			if (_trough == display)return;
			ObjectUtils.disposeObject(_trough);
			_trough = display;
			onPropertiesChanged(P_troughStyle);
		}
		
		
		/**
		 *
		 * 条样式..对..只有一条!
		 *
		 */
		public function get barStyle():String
		{
			return _barStyle;
		}
		
		public function set barStyle(stylename:String):void
		{
			if (stylename == _barStyle)
				return;
			_barStyle = stylename;
			bar = ComponentFactory.Instance.creat(_barStyle)
			onPropertiesChanged(P_barStyle);
		}
		
		/**
		 *
		 * @param display 条
		 *
		 */
		public function set bar(display:DisplayObject):void
		{
			if (_trough == display)return;
			ObjectUtils.disposeObject(_bar);
			_bar = display;
			onPropertiesChanged(P_barStyle);
		}
		
		
		/**
		 *
		 * 遮罩样式...没有你不行..
		 *
		 */
		public function get maskStyle():String
		{
			return _maskStyle;
		}
		
		public function set maskStyle(stylename:String):void
		{
			if (stylename == _maskStyle)
				return;
			_maskStyle = stylename;
			barMask = ComponentFactory.Instance.creat(_maskStyle)
			onPropertiesChanged(P_maskStyle);
		}
		
		/**
		 *
		 * @param display 遮罩
		 *
		 */
		public function set barMask(display:DisplayObject):void
		{
			if (_mask == display)return;
			ObjectUtils.disposeObject(_mask);
			_mask = display;
			onPropertiesChanged(P_maskStyle);
		}
		
		/**
		 *
		 * 文本样式... 可有可无..可以偷懒..
		 *
		 */
		public function get textStyle():String
		{
			return _textStyle;
		}
		
		public function set textStyle(stylename:String):void
		{
			if (stylename == _textStyle)
				return;
			_textStyle = stylename;
			text = ComponentFactory.Instance.creat(_textStyle)
			onPropertiesChanged(P_textStyle);
		}
		
		/**
		 *
		 * @param display 文本
		 *
		 */
		public function set text(display:DisplayObject):void
		{
			if (_text == display)return;
			ObjectUtils.disposeObject(_text);
			_text = display;
			onPropertiesChanged(P_textStyle);
		}
		
		override protected function onProppertiesUpdate():void{
			super.onProppertiesUpdate();
			if (_changedPropeties[P_troughStyle])
			{
				if (_trough && (_trough.width > 0 || _trough.height > 0))
				{
					_width = _trough.width;
					_height = _trough.height;
				}
			}
			
			if(_changedPropeties[P_textInnerRect] && _text)
			{
				var textInnerRect:Rectangle = _textInnerRect.getInnerRect(_width,_height);
				_text.width = textInnerRect.width;
				_text.height = textInnerRect.height;
				_text.x = textInnerRect.x;
				_text.y = textInnerRect.y;
			}
		}
		
		override public function dispose():void{
			
		}
	}
}